<template>
  <div class="app-container">
    <!--查询,按钮-->
    <el-row :gutter="20" class="mb8">
      <el-col :span="5">
        <el-input v-model="searchName" placeholder="模糊搜索用户名"/>
      </el-col>
      <el-col :span="1.5">
        <el-button type="primary" icon="el-icon-search" @click="getUserList()">搜索</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" @click="openInsertDialog = true">添加</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" @click="updatePwdDialog = true">修改个人密码</el-button>
      </el-col>
    </el-row>

    <!-- 表格 -->
    <el-table
      style="margin-top: 20px"
      v-loading="listLoading"
      :data="list"
      element-loading-text="数据加载中"
      border
      fit
      highlight-current-row>

      <el-table-column
        label="序号"
        width="70"
        align="center">
        <template slot-scope="scope">
          {{ (pageNum - 1) * pageSize + scope.$index + 1 }}
        </template>
      </el-table-column>

      <el-table-column prop="account" label="登录名称" width="80"/>
      <el-table-column prop="nickname" label="用户名称" width="100"/>

      <el-table-column prop="createName" label="创建人"/>
      <el-table-column prop="createTime" label="创建日期"/>

      <el-table-column label="操作" width="200" align="center">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" icon="el-icon-edit" @click="findById(scope.row.id)">编辑</el-button>
          <el-button type="danger" size="mini" icon="el-icon-delete" @click="deleteById(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>

    </el-table>

    <!-- 分页 -->
    <el-pagination
      style="padding: 30px 0; text-align: center;"
      @current-change="getUserList"
      :current-page="pageNum"
      :page-size="pageSize"
      layout="prev, pager, next"
      :total="total">
    </el-pagination>


    <el-dialog title="新增用户" width="30%" :visible.sync="openInsertDialog" :close-on-click-modal="false" :modal="true"
               center>
      <el-form :model="userObj" label-width="80px">
        <el-form-item label="账号">
          <el-input v-model="userObj.account" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="手机号">
          <el-input v-model="userObj.mobile" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="昵称">
          <el-input v-model="userObj.nickname" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码">
          <el-input v-model="userObj.password" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="openInsertDialog = false">取 消</el-button>
        <el-button type="primary" @click="saveUser()">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog title="编辑用户" width="30%" :visible.sync="openEditDialog" :close-on-click-modal="false" :modal="true"
               center>
      <el-form :model="editObj" label-width="80px">
        <el-form-item label="账号">
          <el-input v-model="editObj.account" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="手机号">
          <el-input v-model="editObj.mobile" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="昵称">
          <el-input v-model="editObj.nickname" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="openEditDialog = false">取 消</el-button>
        <el-button type="primary" @click="updateUser()">保 存</el-button>
      </div>
    </el-dialog>

    <el-dialog title="修改个人密码" width="30%" :visible.sync="updatePwdDialog" :close-on-click-modal="false" :modal="true"
               center>
      <el-form ref="pwdForm" :model="pwdUser" :rules="rules" label-width="80px">
        <el-form-item label="旧密码" prop="oldPassword">
          <el-input v-model="pwdUser.oldPassword" placeholder="请输入旧密码" type="password"/>
        </el-form-item>
        <el-form-item label="新密码" prop="newPassword">
          <el-input v-model="pwdUser.newPassword" placeholder="请输入新密码" type="password"/>
        </el-form-item>
        <el-form-item label="确认密码" prop="confirmPassword">
          <el-input v-model="pwdUser.confirmPassword" placeholder="请确认密码" type="password"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="updatePwdDialog = false">取 消</el-button>
        <el-button type="primary" @click="updatePwd()">保 存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import userApi from '@/api/system/user'

  export default {
    data() {
      const equalToPassword = (rule, value, callback) => {
        if (this.pwdUser.newPassword !== value) {
          callback(new Error("两次输入的密码不一致"));
        } else {
          callback();
        }
      };
      return {
        pageNum: 1, // 当前页
        pageSize: 3, // 每页记录数
        total: 0, // 总数
        list: null, // 列表
        searchName: '', // 搜索名
        listLoading: true, // 加载图标
        openInsertDialog: false, // 打开增加模态框
        openEditDialog: false, // 打开修改模态框
        updatePwdDialog:false, // 修改密码模态框
        userObj: {}, // 新增对象
        editObj: {}, // 编辑对象
        pwdUser: {  // 修改密码对象
          oldPassword: undefined,
          newPassword: undefined,
          confirmPassword: undefined
        },
        // 表单校验
        rules: {
          oldPassword: [
            { required: true, message: '旧密码不能为空', trigger: 'blur' }
          ],
          newPassword: [
            { required: true, message: '新密码不能为空', trigger: 'blur' },
            { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
          ],
          confirmPassword: [
            { required: true, message: '确认密码不能为空', trigger: 'blur' },
            { required: true, validator: equalToPassword, trigger: 'blur' }
          ]
        }
      }
    },
    created() {
      this.getUserList()
    },
    methods: {
      getUserList(pageNum = 1) {
        this.pageNum = pageNum
        this.listLoading = true
        userApi.getUserList(this.pageNum, this.pageSize, this.searchName).then(resp => {
          this.list = resp.data.records
          this.total = resp.data.total
          this.listLoading = false
        })
      },

      // 保存用户
      saveUser() {
        userApi.addUser(this.userObj).then(resp => {
          this.$message({
            type: 'success',
            message: '新增成功!'
          })
          this.openInsertDialog = false
          this.userObj = {}
          this.getUserList()
        }).catch(resp => {
          this.$message({
            type: 'error',
            message: '新增失败!'
          })
        })
      },
      // 根据id查询
      findById(id) {
        userApi.findById(id).then(resp => {
          this.editObj = resp.data
          this.openEditDialog = true
        }).catch(resp => {
          this.$message({
            type: 'error',
            message: '查询失败!'
          })
        })
      },

      // 修改用户
      updateUser() {
        userApi.updateUser(this.editObj).then(resp => {
          this.$message({
            type: 'success',
            message: '修改成功!'
          })
          this.openEditDialog = false
          this.editObj = {}
          this.getUserList()
        }).catch(resp => {
          this.$message({
            type: 'error',
            message: '修改失败!'
          })
        })

      },
      // 删除
      deleteById(id) {
        this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          userApi.deleteById(id).then(resp => {
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
            this.getUserList()
          }).catch(resp => {
            this.$message({
              type: 'error',
              message: '删除失败!'
            })

          })
        })
      },

      // 修改密码
      updatePwd() {
        let errorOldPwd = false
        this.$refs['pwdForm'].validate(valid => {
          if (valid) {
            // 获取当前用户密码
            userApi.getUserPassWord().then(resp => {
              let currentPwd = resp.data
              if (this.pwdUser.oldPassword !== currentPwd) {
                this.$message({
                  type: 'error',
                  message: '旧密码输入有误!!!'
                })
                errorOldPwd = true
              }
            })
            if (errorOldPwd) {
              return
            }
            userApi.updateUserPwd(this.pwdUser.oldPassword, this.pwdUser.newPassword).then(
              response => {
                if (response.code === 200) {
                  this.$message({
                    type: 'success',
                    message: '修改成功!!'
                  })
                } else {
                  this.$message({
                    type: 'error',
                    message: response.message
                  })
                }
              }
            )
          }
        })
      }
    } // method
  }
</script>
